<?php 
defined('IN_PHPCMS') or exit('NO permission resoures.');
pc_base::load_sys_class('form', '', 0);
pc_base::load_sys_class('format', '', 0);
class index {
	function __construct(){
		$this->username=param::get_cookie('_username');
		if($this->username!=null){
			//echo 'loading db';
			$this->order_db=pc_base::load_model('order_model');
			$this->yubao_db=pc_base::load_model('yubao_model');
			$this->address_db=pc_base::load_model('address_model');
			$this->member_db=pc_base::load_model('member_model');
			$this->goods_db=pc_base::load_model('goods_model');
			$this->chprice_db=pc_base::load_model('chprice_model');
			$this->catgoods_db=pc_base::load_model('catgoods_model');
			//echo 'loaded db';
		}else{
			//exit('here');
			showmessage('请登录后尝试此操作','index.php');
		}
	}
	function init (){
		$username=$this->username;
		$where="username='$username'";
		$data=$this->address_db->select($where);
		$point=$this->member_db->get_one("username='$username'",'point');
		$this->rmb_db=pc_base::load_model('rmb_model');
		$rmb=$this->rmb_db->get_one('id=1');
		$rmb=$rmb['rmb'];
		//print_r($point);
		//print_r($data);
		//echo time();
		include template('order','index');		
	}
	function add_order(){
		//print_r($_POST);
		//exit();		
		if(isset($_POST['dosubmit'])){
			/*
			 * 所有表单都可以用	转运单号关联； 
			 * 
			 */
			$zhnumber=isset($_POST['zhnumber'])?safe_replace($_POST['zhnumber']):exit('zhnumber');
			$un=$this->order_db->get_one("zhnumber='$zhnumber' and state >6",'id');
			if(!isset($un)){
				echo "<script>alert('该转运单号已下过订单了，请先取消订单再重新下单；亲！');window.history.go(-1);</script>";
				exit();
			}
			//订单表
			
			$addorder=Array();
			$addorder['username']=$this->username;
			$addorder['zhnumber']=$zhnumber;
			$addorder['orderid']=time().rand(1000,9999);
			$addorder['channel']=isset($_POST['channel'])?safe_replace($_POST['channel']):exit('channel');
			$addorder['weight']=isset($_POST['weight'])?safe_replace($_POST['weight']):exit('weight');
			$addorder['uptime']=time();
			$addorder['orderprice']='';
			$addorder['whprice']=isset($_POST['whprice'])?safe_replace($_POST['whprice']):exit('whprice');
			$addorder['tariff']=isset($_POST['hfTax'])?safe_replace($_POST['hfTax']):exit('hfTax');
			//$addorder['']=isset($_POST[''])?safe_replace($_POST['']):exit('');
			$addorder['sbvalue']=isset($_POST['sbvalue'])?safe_replace($_POST['sbvalue']):exit('0');
			$addcatgoods['weight']=isset($_POST['weight'])?safe_replace($_POST['weight']):exit('0');
			//$addorder['orderpiont']=isset($_POST['orderpiont'])?safe_replace($_POST['orderpiont']):exit('0');
			$shaddress=isset($_POST['shaddress'])?safe_replace($_POST['shaddress']):exit('shaddress');
			//echo $shaddress.'address id<br>';
			$shaddress=$this->address_db->get_one("id=$shaddress","username,consignee,company,mobile,tel,city,postcode,identitycard,e_address,c_address,ramark");
			//echo $shaddress.'<br>';
			$shaddress=serialize($shaddress);
			//echo $shaddress.'<br>';
			$addorder['shaddress']=$shaddress;
			//exit();
			$addorder['remark']=isset($_POST['remark'])?safe_replace($_POST['remark']):'无备注信息';
			$addorder['reprice']='';//应该根据重量和渠道号计算得出及是否使用优惠卷和积分算出。
			//计算得到的订单价格。
			$channel=$addorder['channel'];
			$weight=ceil($addorder['weight']);
			if($channel==1){
				if($weight<5){
					$price1=$this->chprice_db->get_one("channelid='$channel' and weight='$weight'",'price');
					$price=$price1['price'];
				}else{
					$price1=$this->chprice_db->get_one("channelid='$channel' and weight=4",'price');
					$price2=$this->chprice_db->get_one("channelid='$channel' and weight=5",'price');
					$price=$price1['price']+$price2['price']*($weight-4);
				}
			}else{
				if($weight==1){
					$price1=$this->chprice_db->get_one("channelid='$channel' and weight=1",'price');
					$price=$price1['price'];
				}else{
					$price1=$this->chprice_db->get_one("channelid='$channel' and weight=1",'price');
					$price2=$this->chprice_db->get_one("channelid='$channel' and weight=2",'price');
					$price=$price1['price']+$price2['price']*($weight-1);
				}
				
			}
			$addorder['orderprice']=$price;			
			/*
			 * 订单状态。
			 *  1:代表审核中  2：代表审核完成，等待入库；3:代表入库   4：代表转运途中    5:代表出库    6：结单 7:取消订单。
			 * 
			 */
			$addorder['state']=1;
			$this->order_db->insert($addorder);
			//对到货预报表进行操作
					
			$yborder=Array();
			$yborder['username']=$this->username;
			$yborder['warehouse']=isset($_POST['warehouse'])?safe_replace($_POST['warehouse']):exit('0');
			$yborder['zhcompany']=isset($_POST['zhcompany'])?safe_replace($_POST['zhcompany']):exit('0');
			$yborder['amount']=isset($_POST['amount'])?safe_replace($_POST['amount']):0;
			$yborder['zhnumber']=$zhnumber;
			$yborder['pack']=isset($_POST['pack'])?safe_replace($_POST['pack']):'未填写';
			$yborder['remark']=isset($_POST['remark'])?safe_replace($_POST['remark']):'未填写';
			$yborder['whprice']=isset($_POST['whprice'])?safe_replace($_POST['whprice']):0;
			
			$this->yubao_db->insert( $yborder );
			
			//订单详情表
			$addcatgoods=Array();
			$addcatgoods['zhnumber']=$zhnumber;
			//$addcatgoods['weight']=isset($_POST['weight'])?safe_replace($_POST['weight']):exit('0');
			//得到订单中的货物详情
			$goodscategory=explode("|",$_POST['hfGoodsTax']);
			$goodsname=explode("|",$_POST['hfGoodsName']);
			$goodsprice=explode("|",$_POST['hfGoodsValue']);
			//print_r($goodsname);
			$number_g=count( $goodsname);
			//echo '<br />'.$number_g;
			for($i=0;$i < $number_g;$i++){
				$addcatgoods['category']=$goodscategory[$i];			
				
				$addcatgoods['name']=$goodsname[$i];				
				$addcatgoods['price']=$goodsprice[$i];				
				$addcatgoods['amount']=$number_g;//订单中货物类别个数校对字段；
				
				$this->catgoods_db->insert($addcatgoods);
			}
			
			/*
			 * 写入数据库完成
			 * 跳至库存列表（暂时未写）；
			 * 
			 */
			showmessage('下单成功');
		}else{
			showmessage('请通过正常途经提交表单');
		}

	}
	function show_order(){
		/*
		 * state表示需要选出的订单类型；
		 * $datas 数据；
		 * $pages 为分页
		 * 未使用系统自带分页函数；
		 */
		$pagesize=2;
		$username=$this->username;
		$state=isset($_GET['s'])?"state={$_GET['s']}":1;			
		$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
		$num=$this->order_db->count("username='$username'");
		$mp=max(ceil($num/$pagesize),1);
		$pn=min($pn,$mp);
		$kaishi=($pn-1)*$pagesize;		
		$ps=max($pn-1,1);
		$px=$pn+1;
		$my_sql="select zhcompany,v9_order.zhnumber,pack,weight,uptime,v9_order.remark,v9_yubao.whprice,state from v9_order left join v9_yubao on v9_order.zhnumber=v9_yubao.zhnumber where v9_order.username='$username' and $state order by v9_order.id desc limit $kaishi,$pagesize ";
		//echo $my_sql;
		$res=$this->order_db->query($my_sql);		
		$datas=$this->order_db->fetch_array($res);
		//print_r($datas);		
		if(isset($_GET['s'])){
			$pages="<a href='index.php?m=order&c=index&a=show_order&p=$ps&s={$_GET['s']}'>上一页</><a href='index.php?m=order&c=index&a=show_order&p=$px&s={$_GET['s']}'>下一页</a>";
		}else{
			$pages="<a href='index.php?m=order&c=index&a=show_order&p=$ps'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=index&a=show_order&p=$px'>下一页</a>";
		}
		if($mp==1)unset($pages);
		
		//$pages=pages($num, $curr_page, $perpage = 10, $urlrule = '', $array = array(),$setpages = 10));
		include template('order','showorder');
	}
	function dq_order(){
		/*
		 * 显示当前订单。不包括结单和废单。（已取消的）
		 * 
		 */
		$pagesize=2;
		$username=$this->username;
		$state='state<=5';
		$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
		$num=$this->order_db->count("username='$username'");
		$mp=max(ceil($num/$pagesize),1);
		$pn=min($pn,$mp);
		$kaishi=($pn-1)*$pagesize;
		$ps=max($pn-1,1);
		$px=$pn+1;
		$my_sql="select uptime,orderid,orderprice,state from v9_order where username='$username' and $state order by id desc limit $kaishi,$pagesize ";
		//echo $my_sql;
		$res=$this->order_db->query($my_sql);
		$datas=$this->order_db->fetch_array($res);
		//print_r($datas);		
		$pages="<a href='index.php?m=order&c=index&a=dq_order&p=$ps'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=index&a=dq_order&p=$px'>下一页</a>";		
		if($mp==1)unset($pages);
		include template('order','dqorder');
	}
	function h_order(){
		/*
		 * 显示历史订单
		 * 
		 */
		$pagesize=2;
		$username=$this->username;
		$state='state=6||state=7';
		$_time_k=$_POST['k'];
		$_time_j=$_POST['j'];
		$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
		$num=$this->order_db->count("username='$username' and {$state}");
		$mp=max(ceil($num/$pagesize),1);
		$pn=min($pn,$mp);
		$kaishi=($pn-1)*$pagesize;
		$ps=max($pn-1,1);
		$px=$pn+1;
		$my_sql="select orderid,uptime,state,orderprice from v9_order where username='{$username}' and $state order by id desc limit $pn,$pagesize";		
		//echo $my_sql;
		$res=$this->order_db->query($my_sql);
		$datas=$this->order_db->fetch_array($res);		
		//print_r($datas);
		$pages="<a href='index.php?m=order&c=index&a=h_order&p=$ps'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=index&a=h_order&p=$px'>下一页</a>";
		if($mp==1)unset($pages);		
		include template('order','horder');
	}
	function search_order(){
		/*
		 * 订单查询
		 * $leibie为查询类别，1为订单编号，2为转运单号查询。3则同时搜索两个字段
		 * tbkey 为查询单号
		 * tbstart为开始时间
		 * selcet * from table where zhnumber like %$skey% and uptime>$stime and uptime<$etime
		 */
		//print_r($_POST);
		$username=$this->username;
		if(isset($_POST['dosubmit'])){
			$skey=isset($_POST['tbKey'])?safe_replace($_POST['tbKey']):exit('请开启javascirpt');
			$leibie=isset($_POST['leibie'])?safe_replace($_POST['leibie']):3;
			switch ($leibie){
				case 1:{
					$leibie='orderid';
					$where="$leibie like '%{$skey}%'";
					break;
				}
				case 2:{
					$leibie='zhnumber';
					$where="$leibie like '%{$skey}%'";
					break;
				}
				case 3:{
					$where="orderid like '%{$skey}%' or zhnumber like '%{$skey}%'";
				}
			}
			$stime=isset($_POST['tbStart'])?safe_replace($_POST['tbStart']):'';
			$etime=isset($_POST['tbStart'])?safe_replace($_POST['tbEnd']):'';
			$preg='/^\d{4}-\d{1,2}-\d{1,2}$/';
			if(preg_match($preg,$stime)){
				$stime=explode("-",$stime);
				$stime=mktime(0,0,0,$stime[1],$stime[2],$stime[0]);
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime and uptime>$stime";
				}else{
					$where.="and uptime>$stime";
				}
			}else{
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime";
				}else{
					$where=$where;
				}
			}
			$where.=" and username='{$username}'";
			//echo $where;
			$datas=$this->order_db->select($where);
			//print_r($datas);
		} 
		include template('order','sorder');
	}
	/*
	 * 订单投诉
	 * 
	 */
	function ddts(){
		$this->complaint_db=pc_base::load_model('complaint_model');
		$pagesize=5;
		$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
		$num=$this->complaint_db->count("username='$this->username'");
		$mp=max(ceil($num/$pagesize),1);
		//echo $mp;
		$pn=min($pn,$mp);
		$kaishi=($pn-1)*$pagesize;
		$ps=max($pn-1,1);
		$px=$pn+1;
		$limit="$kaishi,$pagesize";		
		$pages=$pages="<a href='index.php?m=order&c=index&a=ddts&p=$ps'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=index&a=ddts&p=$px'>下一页</a>";
		$datas=$this->complaint_db->select("username='$this->username'",'*',$limit);		
		if(isset($_POST['dosubmit'])){
			$info['username']=$this->username;
			$info['zhnumber']=isset($_POST['zhnumber'])?safe_replace(trim($_POST['zhnumber'])):exit('请提供转运单号');
			$info['reason']=isset($_POST['reason'])?safe_replace($_POST['reason']):exit('请填写原因');
			$info['uptime']=mktime();
			$info['money']=isset($_POST['money'])?safe_replace($_POST['money']):exit('请填写金额');
			$check=$this->order_db->get_one("username='{$info['username']}' and zhnumber='{$info['zhnumber']}'");
			if($check=='')showmessage('你输入的转运单号有误，请确认你的转运单号！');
		//开始上传文件并把文件路径和文件名写入数据库。
		
		if(count($_FILES)){
		$updir="uploadfile/pics/";
		if(!is_dir($updir))mkdir($updir,0777);
		$filename=mktime().rand(100,999);
		$houzhui=explode('.',$_FILES["tbAppendix"]["name"]);
		$houzhui=array_reverse($houzhui);
		$filename.='.'.$houzhui[0];
		//print_r($_FILES);		
		if ((($_FILES["tbAppendix"]["type"] == "image/gif")
				|| ($_FILES["tbAppendix"]["type"] == "image/jpeg")
				|| ($_FILES["tbAppendix"]["type"] == "image/pjpeg"))
				&& ($_FILES["tbAppendix"]["size"] < 2*1024*1024))
  		{
  			if ($_FILES["tbAppendix"]["error"] > 0){
   			 	echo "Return Code: " . $_FILES["tbAppendix"]["error"] . "<br />";
   			 } else	{
   					
    				if (file_exists("$updir" . $filename)){
      					echo $_FILES["tbAppendix"]["name"] . " already exists. ";
     				 } else{
      						move_uploaded_file($_FILES["tbAppendix"]["tmp_name"],
      						$updir.$filename);
      						
     						 }
    			}
  			}else {
  					echo "你上传的文件有误";
  					exit();
  				  }
  			$info['picture']=$updir.$filename;
		}else{
			$info['picture']='';
		}
  			
  			$info['state']='等待审核中';
  			$this->complaint_db->insert($info);
  			
	}
		include template('order','tsdd');
	}
	/*
	 * 税单查询
	 * 
	 * 
	 */
	function sdcx(){
		$this->record_db=pc_base::load_model('record_model');
		if(isset($_POST['dosubmit'])){
			$skey=isset($_POST['tbKey'])?safe_replace($_POST['tbKey']):exit('请开启javascirpt');
			$where="zhnumber like '%{$skey}%'";
			$stime=isset($_POST['tbStart'])?safe_replace($_POST['tbStart']):'';
			$etime=isset($_POST['tbStart'])?safe_replace($_POST['tbEnd']):'';
			$preg='/^\d{4}-\d{1,2}-\d{1,2}$/';
			if(preg_match($preg,$stime)){
				$stime=explode("-",$stime);
				$stime=mktime(0,0,0,$stime[1],$stime[2],$stime[0]);
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime and uptime>$stime";
				}else{
					$where.="and uptime>$stime";
				}
			}else{
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime";
				}else{
					$where=$where;
				}
			}
			$where.=" and username='{$username}'";
			//echo $where;
			$datas=$this->record_db->select($where);			
			
		}else{
			$pagesize=5;
			$where="username='$this->username'";
			$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
			$num=$this->record_db->count("$where");
			$mp=max(ceil($num/$pagesize),1);
			//echo $mp;
			$pn=min($pn,$mp);
			$kaishi=($pn-1)*$pagesize;
			$ps=max($pn-1,1);
			$px=$pn+1;
			//echo 'here_';
			$limit="$kaishi,$pagesize";
			$datas=$this->record_db->select($where,'*',$limit,$order='id desc');
			//print_r($datas);
			$pages="<div style=\"margin:10px 0 10px 200px \">共计{$num}条<a href='index.php?m=order&c=index&a=sdzf&p=$ps'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=index&a=sdzf&p=$px'>下一页</a></div>";
			if($mp==1)unset($pages);
		}				
		include template('order','sdcx');
	}
	/*
	 * 税单支付
	 * 
	 */
	function sdzf(){
		$username=$this->username;
		if(isset($_POST['dosubmit'])){
			$skey=isset($_POST['tbKey'])?safe_replace($_POST['tbKey']):exit('请开启javascirpt');
			$leibie=isset($_POST['leibie'])?safe_replace($_POST['leibie']):3;
			switch ($leibie){
				case 1:{
					$leibie='orderid';
					$where="$leibie like '%{$skey}%'";
					break;
				}
				case 2:{
					$leibie='zhnumber';
					$where="$leibie like '%{$skey}%'";
					break;
				}
				case 3:{
					$where="orderid like '%{$skey}%' or zhnumber like '%{$skey}%'";
				}
			}
			$stime=isset($_POST['tbStart'])?safe_replace($_POST['tbStart']):'';
			$etime=isset($_POST['tbStart'])?safe_replace($_POST['tbEnd']):'';
			$preg='/^\d{4}-\d{1,2}-\d{1,2}$/';
			if(preg_match($preg,$stime)){
				$stime=explode("-",$stime);
				$stime=mktime(0,0,0,$stime[1],$stime[2],$stime[0]);
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime and uptime>$stime";
				}else{
					$where.="and uptime>$stime";
				}
			}else{
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime";
				}else{
					$where=$where;
				}
			}
			$where.=" and username='{$username}'";
			//echo $where;
			$datas=$this->order_db->select($where);			
			
		}else{
			$pagesize=5;
			$where="username='$username' and state!=7 and ispay='否'";
			$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
			$num=$this->order_db->count("$where");
			$mp=max(ceil($num/$pagesize),1);
			//echo $mp;
			$pn=min($pn,$mp);
			$kaishi=($pn-1)*$pagesize;
			$ps=max($pn-1,1);
			$px=$pn+1;
			$limit="$kaishi,$pagesize";
			$datas=$this->order_db->select($where,'*',$limit);
			$pages="<div style=\"margin:10px 0 10px 200px \">共计{$num}条<a href='index.php?m=order&c=index&a=sdzf&p=$ps'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=index&a=sdzf&p=$px'>下一页</a></div>";
			if($mp==1)unset($pages);
		}
		include template('order','sdzf');
		
	}
	function zhifu(){
		$zhnumber=isset($_REQUEST['zhnumber'])?safe_replace($_REQUEST['zhnumber']):exit('系统错误');
		$where="zhnumber='$zhnumber'";		
		$data_2=$this->yubao_db->get_one($where);
		$data_3=$this->catgoods_db->select($where);
		$leishu=$this->catgoods_db->count($where);
		if($_POST['zhifu']){
			$data_1=$this->order_db->get_one($where);
			$zongjia=$data_1['orderprice']+$data_1['tariff']+$data_2['whprice'];
			$info=$this->member_db->get_one("username='$this->username'");
			if($info['amount']<$zongjia){
				showmessage('您的账户余额不足，请充值。','index.php?m=pay&c=deposit&a=pay');
			}
			$info['amount']=$info['amount']-$zongjia;
			$res=$this->member_db->update("amount={$info['amount']}","username='$this->username'");
			if($res){
				$this->order_db->update("ispay='是'",$where);
				$this->record_db=pc_base::load_model('record_model');
				$addrecord['username']=$this->username;
				$addrecord['zhnumber']=$zhnumber;
				$addrecord['uptime']=mktime();
				$addrecord['orderprice']=$data_1['orderprice'];
				$addrecord['tariff']=$data_1['tariff'];
				$addrecord['whprice']=$data_2['whprice'];
				$addrecord['pay']=$zongjia;
				$this->record_db->insert($addrecord);				
				
			}
			showmessage('支付成功','index.php?m=order&c=index&a=sdcx');			
		}
		$data_1=$this->order_db->get_one($where);
		$address=unserialize($data_1['shaddress']);
		//print_r($address);
		$zongjia=$data_1['orderprice']+$data_1['tariff']+$data_2['whprice'];
		include template('order','zhifu');
		
	}
	/*
	 * 公共查询方法
	 * 
	 */
	
	
}







?>